package Frame.shopSet;

import FCRendering.FCRMaster;
import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import Function.FCMutilPlayCache;
import VWorldGameStudio.FCItem;
import VWorldGameStudio.FCPlayerInfo;
import VWorldGameStudio.FCRPlayerLink;
import VWorldGameStudio.FCTank;
import VWorldGameStudio.events.ShopEvent;

import java.util.Vector;

import static FCRendering.FCRMaster.*;

/**
 * 的商店点击事件和显示都在这里做了
 */
public class FunctionBarShop extends AShopFuntion {
    private Vector<String> itemNames;
    private Vector<Integer> as = new Vector<>();
    private int showFristIndex = 0;

    private int endI = 0;

    private int willBuyPerPrice = 0;
    private String willBuyItemName = "null";

    private void setEndIndex(int num) {
        endI = num;
    }

    public FunctionBarShop() {
        buildShopWindow();
    }


    public void buildShopWindow() {
        itemNames = ((ShopEvent) fcv.getPlayingEvent()).getItemid();
        for (String name : itemNames) {
            as.add(itemLib.getItemOfID(name).getBuyInPrice());
        }
        setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        int m = 0;
        for (int i = showFristIndex; i < endI; i++) {
            FCRValues.leftWindowUI[m].leftText = itemLib.getItemOfID(itemNames.get(i)).getChName();
            FCRValues.leftWindowUI[m].rightText = as.get(i) + "G";
            m++;
        }

        FCRValues.rightWindowUI[1].leftText = "T:一饮而尽吧，或许有buff呢";
        FCRValues.rightWindowUI[3].rightText = "上一页";
        FCRValues.rightWindowUI[4].rightText = "下一页";
        FCRValues.rightWindowUI[5].rightText = "干了";
        FCRValues.rightWindowUI[6].rightText = "不喝了";
    }

    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        //永远都是0到6
        if (entity.leftText.length() > 1) {
            FCItem item = itemLib.getItemOfID(itemNames.get(showFristIndex + i));
            if (item != null) {
                willBuyPerPrice = as.get(showFristIndex + i);
                willBuyItemName = item.getName();
                FCRValues.rightWindowUI[0].leftText = "一杯 "+item.getChName();
                FCRValues.rightWindowUI[1].leftText = item.getDepict();
            }
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));//保留这句代码
        switch (i) {
            case 3:
                if (showFristIndex >= 7) {
                    showFristIndex -= 7;
                    setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));
                    resetUI();
                }
                break;
            case 4:
                if (endI < itemNames.size()) {
                    showFristIndex += 7;
                    setEndIndex(Math.min(itemNames.size(), showFristIndex + 7));
                    resetUI();
                }
                break;
            case 5:
                if (willBuyItemName != null) {
                    buyItWithMoney(willBuyPerPrice);
                } else {
                    FCRValues.rightWindowUI[1].leftText = "T:请先选择酒水";
                }
                break;
            case 6://取消
                fcv.setOpenSayShop(false);
                FCRMaster.fcv.setOpenSay(false);
                FCRMaster.fcv.setSayNpcName(null);
                FCRMaster.fcv.setSayNpcTextNum(0);
                break;
        }
    }


    private void buyItWithMoney(int needMoney) {
        if (player1.getMoney() >= needMoney) {
            //检查有没有空背包
            FCItem item = itemLib.getItemOfID(willBuyItemName);
            player1.setMoney(player1.getMoney() - needMoney);
            fcv.getMessage().add(player1.getChName() + " 喝了一杯" + item.getChName() + "，精神多了");
            fcv.showMessage();
        } else {
            FCRValues.rightWindowUI[1].leftText = "T:不够钱了!";
        }
    }
}
